package com.example.zx.theline.lyjh.lyyjh.pub;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;
import com.example.zx.theline.lyjh.pub.JhshHistoryActivity;
import com.example.zx.theline.pub.CheckInfoActivity;

import java.util.ArrayList;
import java.util.List;

import GsonBean.LjyyxShdwFzrdh;
import GsonBean.ShyjhxqBean;
import GsonBean.SuccessBean;
import GsonBean.YjhshthBean;
import GsonBean.post.SgyjhshPost;
import Utils.DateUtil;
import Utils.MyToast;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseActivity;
import dialog.CenterDialog;
import dialog.CustomDialog;
import dialog.ThDialog;
import dialog.ZdfzrDialog;
import greendao.entity.User;
import network.NetWorkApi;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 计划审核查看详情页面 审核
 */
public class JhshShActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    public static GsonBean.ShyjhxqBean ZsyjhxxShBean;
    //标题
    private TextView centerTextView;
    //计划内容
    private TextView lb, yf, ydbh, kssj, xm, dd, hb, lc, xmmc, sgnr, sgjx, blfh;
    // 相关单位
    private TextView sgdw, sgdwfzr, jldw, jldwfzr, jsdw, cwzd, sbgldw, qtdw, shcs, xmzgbm;
    // 按钮
    private TextView tv_shenhe, tv_chakan, tv_cancel, tv_zdfzr, tv_shliucheng;
    private TextView rightTextView, leftTextView;
    private CheckBox checkbox;
    private CustomDialog customDialog;
    private ThDialog mThDialog;
    private CenterDialog mCenterDialog;
    //新增监护人 dialog
    private ZdfzrDialog mZdfzrDialog;
    private TextView cancelBtn, confirmBtn; //弹出框按钮
    private RadioGroup mRadioGroup;
    private RadioButton mAgreeBtn, mDisAgreeBtn, mAgreeUpdateBtn;
    private EditText mEditText;
    private TextView mShyjTv;

    private Intent intent;

    SgyjhshPost mSgyjhshPost = new SgyjhshPost();
    YjhshthBean mYjhshthBean = new YjhshthBean();

    ArrayList<User> mSelectUserList = new ArrayList<User>();

    //审核单位列表
    private List<ShyjhxqBean.InfoBean.ShdwListBean> mList;

    private boolean NeedZdfzr = false;

    private LinearLayout blfh_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jhsh_chakan);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {

        centerTextView = (TextView) findViewById(R.id.centerTextView);
        centerTextView.setText("月计划详情");

        lb = (TextView) findViewById(R.id.lb);
        yf = (TextView) findViewById(R.id.yf);
        ydbh = (TextView) findViewById(R.id.ydbh);
        kssj = (TextView) findViewById(R.id.kssj);
        xm = (TextView) findViewById(R.id.xm);
        dd = (TextView) findViewById(R.id.dd);
        hb = (TextView) findViewById(R.id.hb);
        lc = (TextView) findViewById(R.id.lc);
        xmmc = (TextView) findViewById(R.id.xmmc);
        sgnr = (TextView) findViewById(R.id.sgnr);
        sgjx = (TextView) findViewById(R.id.sgjx);
        blfh = (TextView) findViewById(R.id.blfh);

        sgdw = (TextView) findViewById(R.id.sgdw);
        sgdwfzr = (TextView) findViewById(R.id.sgdwfzr);
        jldw = (TextView) findViewById(R.id.jldw);
        jldwfzr = (TextView) findViewById(R.id.jldwfzr);
        jsdw = (TextView) findViewById(R.id.jsdw);
        cwzd = (TextView) findViewById(R.id.cwzd);
        sbgldw = (TextView) findViewById(R.id.sbgldw);
        qtdw = (TextView) findViewById(R.id.qtdw);
        shcs = (TextView) findViewById(R.id.shcs);
        xmzgbm = (TextView) findViewById(R.id.xmzgbm);
        blfh_layout = (LinearLayout) findViewById(R.id.blfh_layout);

        tv_shenhe = (TextView) findViewById(R.id.tv_shenhe);
        tv_zdfzr = (TextView) findViewById(R.id.tv_zdfzr);
        tv_chakan = (TextView) findViewById(R.id.tv_chakan);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_shliucheng = (TextView) findViewById(R.id.tv_shliucheng);

        leftTextView = (TextView) findViewById(R.id.leftTextView);
        rightTextView = (TextView) findViewById(R.id.rightTextView);
        //初始化Dialog
        customDialog = new CustomDialog(this, R.layout.dialog_check_info, R.style.Theme_dialog);
        customDialog.setCancelable(false);
        checkbox = (CheckBox) customDialog.findViewById(R.id.checkbox);
        cancelBtn = (TextView) customDialog.findViewById(R.id.btn_cancel);
        confirmBtn = (TextView) customDialog.findViewById(R.id.btn_confirm);
        mRadioGroup = (RadioGroup) customDialog.findViewById(R.id.dialog_radio_group);
        mAgreeBtn = (RadioButton) customDialog.findViewById(R.id.rb_agree);
        mAgreeUpdateBtn = (RadioButton) customDialog.findViewById(R.id.rb_agree_update);
        mDisAgreeBtn = (RadioButton) customDialog.findViewById(R.id.rb_dis_agree);
        mEditText = (EditText) customDialog.findViewById(R.id.edit_check_info);
        mShyjTv = (TextView) customDialog.findViewById(R.id.tv_shyj);

//        根据 yjhDwlb == "shcs" && spcsmc(月计划项目主管部门) == 登录单位名称 && shsftg == 0（都是同意）则显示 否则 隐藏。
        if (ZsyjhxxShBean.getInfo().getYjhDwlb() != null && ZsyjhxxShBean.getInfo().getYjhDwlb().equals("shcs")
                && ZsyjhxxShBean.getInfo().getSpcsmc().equals(MyApplication.getmLoginUser().getUserUnitName())
                && ZsyjhxxShBean.getInfo().getShsftg().equals("0")) {
            checkbox.setVisibility(View.VISIBLE);
        } else {
            checkbox.setVisibility(View.GONE);
        }
    }

    @Override
    protected void InitData() {
        rightTextView.setVisibility(View.GONE);
        if (ZsyjhxxShBean == null) {
            MyApplication.ShowWaitDialog(this);
            MyApplication.setWaitDialogMsg("数据错误");
        } else {
            mList = ZsyjhxxShBean.getInfo().getShdwList();
            lb.setText(ZsyjhxxShBean.getInfo().getLb());
            yf.setText(ZsyjhxxShBean.getInfo().getSgny());
            ydbh.setText(ZsyjhxxShBean.getInfo().getBh());

            //施工时间
            kssj.setText(ZsyjhxxShBean.getInfo().getExsgrq());
            xm.setText(ZsyjhxxShBean.getInfo().getXlmc());
            dd.setText(ZsyjhxxShBean.getInfo().getXlxbList().get(0).getSgdd());
            hb.setText(ZsyjhxxShBean.getInfo().getXlxbList().get(0).getXbmc());
            lc.setText(ZsyjhxxShBean.getInfo().getXlxbList().get(0).getLic());
            xmmc.setText(ZsyjhxxShBean.getInfo().getXmmc());
            sgnr.setText(ZsyjhxxShBean.getInfo().getNrmc());
            sgjx.setText(ZsyjhxxShBean.getInfo().getJxmc());
            if (ZsyjhxxShBean.getInfo().getLb().equals("C")) {
                blfh_layout.setVisibility(View.GONE);
            } else {
                blfh_layout.setVisibility(View.VISIBLE);
                blfh.setText(ZsyjhxxShBean.getInfo().getFhcsmc());
            }

            sgdw.setText(ZsyjhxxShBean.getInfo().getSgdwmc()); //施工单位名称
            sgdwfzr.setText(ZsyjhxxShBean.getInfo().getSgdwfzr());
            jldw.setText(ZsyjhxxShBean.getInfo().getJldw());
            jldwfzr.setText(ZsyjhxxShBean.getInfo().getJldwfzr());
            jsdw.setText(ZsyjhxxShBean.getInfo().getJsdwmc()); //建设单位名称
            cwzd.setText(ZsyjhxxShBean.getInfo().getCwzd());
            sbgldw.setText(ZsyjhxxShBean.getInfo().getSbgldw());
            qtdw.setText(ZsyjhxxShBean.getInfo().getQtdw());
            shcs.setText(ZsyjhxxShBean.getInfo().getShcsmc()); //审核处室名称
            xmzgbm.setText(ZsyjhxxShBean.getInfo().getSpcsmc()); //审批处室名称

            if (ZsyjhxxShBean.getInfo().isIsZdShdw()) {
                tv_zdfzr.setVisibility(View.VISIBLE);
                NeedZdfzr = true;
                mCenterDialog = new CenterDialog(this, new CenterDialog.CenterClick() {
                    @Override
                    public void confirmListener() {
                        mCenterDialog.dismiss();
                        showZdfzrDialog();
                    }

                    @Override
                    public void cancelListener() {

                    }
                });
                mCenterDialog.setMessage("由于您是新增单位，请指定一个负责人!");
                mCenterDialog.show();
            } else {
                tv_zdfzr.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void InitListen() {
        tv_shenhe.setOnClickListener(this);
        tv_chakan.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        leftTextView.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        mRadioGroup.setOnCheckedChangeListener(this);
        tv_zdfzr.setOnClickListener(this);
        tv_shliucheng.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //审核
            case R.id.tv_shenhe:
                if (NeedZdfzr) {
                    Toast.makeText(JhshShActivity.this, "请指定负责人", Toast.LENGTH_SHORT).show();
                    return;
                }
                customDialog.show();
                break;
            //审核流程
            case R.id.tv_shliucheng:
                CheckInfoActivity.ZsyjhxxShBean = ZsyjhxxShBean;
                intent = new Intent(JhshShActivity.this, CheckInfoActivity.class);
                startActivity(intent);
                break;
            //查看历史
            case R.id.tv_chakan:
                JhshHistoryActivity.ZsyjhxxShBean = ZsyjhxxShBean;
                intent = new Intent(JhshShActivity.this, JhshHistoryActivity.class);
                startActivity(intent);
                break;
            //返回
            case R.id.leftTextView:
                finish();
                break;
            //弹出框
            case R.id.btn_cancel:
                customDialog.dismiss();
                break;
            case R.id.btn_confirm:
                //审核月计划
                if(mEditText.getText().toString().trim().length()==0&&mEditText.getText().toString().length()>0&&!mAgreeBtn.isChecked())
                {
                    Toast.makeText(JhshShActivity.this, "意见请不要输入空格", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mAgreeUpdateBtn.isChecked() || mDisAgreeBtn.isChecked()) {
                    if (mEditText.getText().toString().trim().length() == 0) {
                        Toast.makeText(JhshShActivity.this, "请填写审核意见", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if (!mAgreeUpdateBtn.isChecked() && !mDisAgreeBtn.isChecked() && !mAgreeBtn.isChecked()) {
                    Toast.makeText(JhshShActivity.this, "请选择是否通过", Toast.LENGTH_SHORT).show();
                    return;
                }

                mSgyjhshPost.setJdjhbBh(ZsyjhxxShBean.getInfo().getBh());
                mSgyjhshPost.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                if (mAgreeBtn.isChecked()) {
                    mSgyjhshPost.setSftg("Y");
                }
                if (mAgreeUpdateBtn.isChecked()) {
                    mSgyjhshPost.setSftg("YH");
                    mSgyjhshPost.setShyj(mEditText.getText().toString());
                }
                if (mDisAgreeBtn.isChecked()) {
                    mSgyjhshPost.setSftg("N");
                    mSgyjhshPost.setShyj(mEditText.getText().toString());
                }
                mSgyjhshPost.setShzt(ZsyjhxxShBean.getInfo().getShzt());
                mSgyjhshPost.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                mSgyjhshPost.setShrname(MyApplication.getmLoginUser().getTrueName());
                mSgyjhshPost.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                mSgyjhshPost.setShrq(DateUtil.getCurDate());
                mSgyjhshPost.setDwlb(ZsyjhxxShBean.getInfo().getYjhDwlb());
                mSgyjhshPost.setShzt(ZsyjhxxShBean.getInfo().getShzt());
                //判断条件：dwlb=='shcs' &&spcsmc（月计划审批处室）==当前登录单位名称选中则为1，否则为0，否则取月计划shsubmit值（绑定原值）。
                if (checkbox.getVisibility() == View.GONE) {
                    mSgyjhshPost.setShsubmit(ZsyjhxxShBean.getInfo().getShsubmit());
                }
                if (checkbox.getVisibility() == View.VISIBLE) {
                    if (checkbox.isChecked()) {
                        mSgyjhshPost.setShsubmit("1");
                    } else {
                        mSgyjhshPost.setShsubmit("0");
                    }
                }
                MyApplication.ShowWaitDialog(this);
                NetWorkApi.shyjh(mSgyjhshPost, new Callback<SuccessBean>() {
                            @Override
                            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                try {
                                    SuccessBean Bean = response.body();
                                    if (Bean.getMsg().equals("success")) {
                                        MyToast.showToast("审核成功");
                                        RefreshManager.Sgyjh.RefreshShyjh();
                                        RefreshManager.Sgyjh.RefreshShywc();
                                        customDialog.dismiss();
                                        JhshShActivity.this.finish();
                                    }
                                } catch (Exception e) {
                                    MyToast.showFail();
                                }
                                MyApplication.HideWaitDialog();
                            }

                            @Override
                            public void onFailure(Call<SuccessBean> call, Throwable t) {
                                MyToast.showFail();
                            }
                        }
                );
                break;

            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_zdfzr:
                showZdfzrDialog();
                break;
            default:
                break;
        }
    }

    //  显示新增负责人dialog
    public void showZdfzrDialog() {
        if (mZdfzrDialog == null) {
            mZdfzrDialog = new ZdfzrDialog(this, new ZdfzrDialog.onClickListen() {
                @Override
                public void onConfirmListen(ArrayList<User> SelectUserList) {
                    if (SelectUserList.size() == 0) {
                        MyToast.showToast("请指定至少一个负责人");
                        return;
                    } else {
                        mSelectUserList = SelectUserList;
                        //审核单位负责人
                        List<LjyyxShdwFzrdh> shdwFzrList = new ArrayList<>();
                        for (int i = 0; i < mSelectUserList.size(); i++) {
                            User mUser = mSelectUserList.get(i);
                            LjyyxShdwFzrdh mLjyyxShdwFzrdh = new LjyyxShdwFzrdh();
                            mLjyyxShdwFzrdh.setJdjhbBh(ZsyjhxxShBean.getInfo().getBh());
                            mLjyyxShdwFzrdh.setShdwId(MyApplication.getmLoginUser().getUserUnitId());
                            mLjyyxShdwFzrdh.setFzrId(mUser.getLjyyxWorkerName());
                            mLjyyxShdwFzrdh.setFzrdh(mUser.getLjyyxWorkerTel());
                            mLjyyxShdwFzrdh.setDwlb(ZsyjhxxShBean.getInfo().getYjhDwlb());
                            mLjyyxShdwFzrdh.setDeleteFlag("N");
                            mLjyyxShdwFzrdh.setCjrId(MyApplication.getmLoginUser().getUserDisplayName());
                            mLjyyxShdwFzrdh.setCjrq(DateUtil.getCurDate());
                            mLjyyxShdwFzrdh.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());
                            mLjyyxShdwFzrdh.setXgrq(DateUtil.getCurDate());
                            shdwFzrList.add(mLjyyxShdwFzrdh);
                        }
                        mSgyjhshPost.setShdwFzrList(shdwFzrList);
                        //设置完新增负责人以后更新到页面
                        //监理单位新增负责人
                        if (ZsyjhxxShBean.getInfo().getYjhDwlb().equals("jldw")) {
                            StringBuffer s = new StringBuffer();
                            for (int i = 0; i < mSelectUserList.size(); i++) {
                                User user = mSelectUserList.get(i);
                                s.append(user.getLjyyxWorkerName())
                                        .append("：")
                                        .append(user.getLjyyxWorkerTel())
                                        .append("，");

                            }
                            s.deleteCharAt(s.length() - 1);
                            jldwfzr.setText(s.toString());

                            mSgyjhshPost.setJldwfzr(jldwfzr.getText().toString());
                        } else {
                            //设备管理单位新增负责人
                            StringBuffer s = new StringBuffer();
                            for (int i = 0; i < ZsyjhxxShBean.getInfo().getShdwList().size(); i++) {
                                if (ZsyjhxxShBean.getInfo().getShdwList().get(i).getDwlb().equals("sbgldw")) {
                                    s.append(ZsyjhxxShBean.getInfo().getShdwList().get(i).getDwmc())
                                            .append("-");
                                    if (ZsyjhxxShBean.getInfo().getShdwList().get(i).getShdwFzrList().size() == 0) {
                                        for (User user : mSelectUserList) {
                                            s.append(user.getLjyyxWorkerName())
                                                    .append("：")
                                                    .append(user.getLjyyxWorkerTel())
                                                    .append("，");
                                        }
                                        s.deleteCharAt(s.length() - 1);
                                        s.append("；");
                                    } else {
                                        for (int j = 0; j < ZsyjhxxShBean.getInfo().getShdwList().get(i).getShdwFzrList().size(); j++) {
                                            ShyjhxqBean.InfoBean.ShdwListBean.ShdwFzrListBean Bean = ZsyjhxxShBean.getInfo().getShdwList().get(i).getShdwFzrList().get(j);
                                            s.append(Bean.getFzrId())
                                                    .append("：")
                                                    .append(Bean.getFzrdh())
                                                    .append("，");
                                        }
                                        s.deleteCharAt(s.length() - 1);
                                        s.append("；");
                                    }
                                }
                            }
                            s.deleteCharAt(s.length() - 1);
                            sbgldw.setText(s.toString());
                            mSgyjhshPost.setSbdwfzr(sbgldw.getText().toString());
                        }
                        mZdfzrDialog.dismiss();
                        MyToast.showToast("指定负责人成功");
                        NeedZdfzr = false;
                    }
                }
            });

        }
        mZdfzrDialog.setUserList(mSelectUserList);
        mZdfzrDialog.show();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { //单选按钮组选中事件
        switch (checkedId) {
            case R.id.rb_agree:
                mShyjTv.setVisibility(View.GONE);
                mEditText.setVisibility(View.GONE);
                break;
            case R.id.rb_agree_update:
                mShyjTv.setVisibility(View.VISIBLE);
                mEditText.setVisibility(View.VISIBLE);
                break;
            case R.id.rb_dis_agree:
                mShyjTv.setVisibility(View.VISIBLE);
                mEditText.setVisibility(View.VISIBLE);
                break;
        }
    }

}
